import Vue from 'vue'
import Router from 'vue-router'
import iView from 'iview'
import home from './views/home.vue'
import admin_home from './views/admin/home.vue'
Vue.use(Router)
let router = new Router({
	mode: 'history',
	routes: [{
			path: '/',
			name: 'home',
			component: home,
			children: [{
					path: '/index',
					name: 'index',
					component: () =>
						import('./views/index.vue'),
				},
				{
					path: '/article',
					name: 'article',
					component: () =>
						import('./views/article.vue'),
				}
			]

		},
		{
			path: '/login',
			name: 'login',
			component: () =>
				import('./views/login.vue'),
		},
		{
			path: '/admin/login',
			name: 'admin_login',
			component: () =>
				import('./views/admin/login.vue'),
		},
		{
			path: '/admin/',
			name: 'admin_home',
			component: admin_home,
			children: [{
				path: '/admin/',
				name: 'index',
				component: () =>
					import('./views/admin/index.vue'),
			}, {
				path: '/admin/index',
				name: 'admin_index',
				component: () =>
					import('./views/admin/index.vue'),
			}, {
				path: '/admin/user',
				name: 'user',
				component: () =>
					import('./views/admin/user.vue'),
			}, {
				path: '/admin/user/add',
				name: 'user_add',
				component: () =>
					import('./views/admin/user_add.vue'),
			}, {
				path: '/admin/user/editor',
				name: 'user_editor',
				component: () =>
					import('./views/admin/user_editor.vue'),
			}, {
				path: '/admin/article',
				name: 'article',
				component: () =>
					import('./views/admin/article.vue'),
			}, {
				path: '/admin/article/add',
				name: 'article_add',
				component: () =>
					import('./views/admin/article_add.vue'),
			}, {
				path: '/admin/article/edit',
				name: 'article_edit',
				component: () =>
					import('./views/admin/article_edit.vue'),
			}, {
				path: '/admin/music',
				name: 'music',
				component: () =>
					import('./views/admin/music.vue'),
			}, {
				path: '/admin/link',
				name: 'link',
				component: () =>
					import('./views/admin/link.vue'),
			}, {
				path: '/admin/about',
				name: 'about',
				component: () =>
					import('./views/admin/about.vue'),
			}, {
				path: '/admin/menu',
				name: 'menu',
				component: () =>
					import('./views/admin/menu.vue'),
			}]
		},
	],
});

router.beforeEach((to, from, next) => {
	var token = sessionStorage.getItem('token');
	sessionStorage.setItem("token", "this is token")
	if(token) { //如果有就直接到首页咯
		if(to.path == '/login' || to.path == '/admin/login') { //如果是登录页面路径，就直接next()
			next("/index");
			return;
		}
		next();
		return;
	} else {
		if(to.path == '/login' || to.path == '/admin/login') { //如果是登录页面路径，就直接next()
			next();
			return;
		} else { //不然就跳转到登录；
			if(to.path.length > 5 && to.path.substring(1, 6) == 'admin') {
				next('/admin/login');
				return;
			} else {
				next('/login');
				return;
			}
		}
	}
});
router.afterEach((to) => {
	iView.LoadingBar.finish();
	window.scrollTo(0, 0);
});

export default router